import random
import re

from django.db.models import Q
from rest_framework.response import Response
from rest_framework.views import APIView
from login.models import User
from utils import redis
from utils.jwttool import tooljwt
from .tasks import send_sms
# Create your views here.
class LoginByPsw(APIView):
    def post(self,request):
        account=request.data.get('account',None)
        password=request.data.get('password',None)
        if not account or not password:
            return Response({"msg":"账号或密码不能为空"})
        user=User.objects.filter(Q(username=account)|Q(phone=account)|Q(email=account)).first()
        if not user:
            return Response({"msg":"账号不存在","code":400})
        if user.password != password:
            return Response({"msg":"密码错误","code":400})
        token=tooljwt.getjwt({"user":user.id})
        return Response({"msg":"登录成功","code":200,"data":{"user":user.id,"token":"Bearer "+token}})
from utils.sendmsg import send_message
import random
from utils.redis import toolredis
class Send(APIView):
    def get(self,request):
        phone=request.query_params.get("phone",)
        re_phone=re.match(r"^1[3-9]\d{9}$",phone)
        if not re_phone:
            return Response({"msg":"手机号格式错误","code":400})
        if toolredis.getcode(phone):
            return Response({"msg":"验证码已发送,请稍后再试","code":400})
        code=str(random.randint(1000,9999))
        task=send_sms.delay(phone,code)
        res=task.get()
        task.wait()
        if res["statusCode"]=="000000":
            toolredis.setcode(phone,code)
            return Response({"msg":"验证码发送成功","code":200})
        else:
            return Response({"msg":"验证码发送失败","code":400})

class Login(APIView):
    def post(self,request):
        phone=request.data.get('phone',None)
        code=request.data.get('code',None)
        if not phone or not code:
            return Response({"msg":"手机号或验证码不能为空","code":400})
        rediscode=toolredis.getcode(phone)
        if rediscode != code:
            return Response({"msg":"验证码错误","code":400})
        user=User.objects.filter(phone=phone).first()
        if not user:
            user=User.objects.create(phone=phone)
        token=tooljwt.getjwt({"user":user.id})
        return Response({"msg":"登录成功","code":200,"data":{"user":user.id,"token":"Bearer "+token}})
class Register(APIView):
    def post(self,request):
        phone=request.data.get('phone',None)
        code=request.data.get('code',None)
        if not  phone or not code:
            return Response({"msg":"参数不完整","code":400})
        rediscode=toolredis.getcode(phone)
        if rediscode != code:
            return Response({"msg":"验证码错误","code":400})
        user=User.objects.filter(phone=phone).first()
        if user:
            return Response({"msg":"手机号已注册，直接登录即可","code":400})
        user=User.objects.create(phone=phone,password=request.data.get('password',None))
        if user:
            token=tooljwt.getjwt({"user":user.id})
            return Response({"msg":"注册成功","code":200,"data":{"user":user.id,"token":"Bearer "+token}})
        return Response({"msg":"注册失败","code":400})

